﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using Exhibition.Common.Helper;
using Exhibition.Dao.ViewModel;
using Exhibition.RenderTemplete.Controls;
using Exhibition.Service;

namespace Exhibition.Manage
{
    public partial class UserList : BaseAdminPage
    {
        //是否全部数据
        bool isAll = false;
        //是否显示自己的数据
        bool isOwner = false;
        string isDel = string.Empty;
        public int tpid = 0;
        string table = "E_User";
        public int chk = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["tp"] != null) {
                tpid = Convert.ToInt32(Request["tp"]);
            }
            if (Request["ck"]!=null) {
                chk = Convert.ToInt32(Request["ck"]);
            }

            if (!IsPostBack) {
                grid = RenderGridView.BuildASPXGridView(grid, table, ref isAll, ref isOwner);
               
            }
            LoadUser();
        }

        private void LoadUser()
        {
            int recordcount = 0;
            grid.KeyFieldName = "Id";
            string strWhr = string.Empty;
            if (chk > 0) {
                strWhr = " and IsCheck="+chk;
            }
            List<UserModel> usermdls = CenterService.userService.GetUserList(strWhr, tpid, pager.PageIndex, pager.ItemsPerPage, out recordcount);
            pager.ItemCount = recordcount;
            grid.DataSource = usermdls;
            grid.DataBind();
        
        }
        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grid_BeforeColumnSortingGrouping(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewBeforeColumnGroupingSortingEventArgs e)
        {
            RenderGridView.ColumnSortingGrouping(e);
            LoadUser();
        }


        protected void btnDelete_Click(object sender, EventArgs e)
        {
            List<object> lisDel = grid.GetSelectedFieldValues(new string[] { grid.KeyFieldName });
            string redUrl = string.Empty;
            foreach (var obj in lisDel)
            {
                try
                {
                    
                    if (!string.IsNullOrEmpty(isDel))
                    {
                        CenterService.operatingDataService.DeleteData(Convert.ToInt32(obj), table);

                    }
                    else
                    {
                        CenterService.operatingDataService.ExecuteNonQuery("DELETE FROM " + table + " WHERE ID=" + Convert.ToInt32(obj));

                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error(CommFunMsg.GetMessage("e0001", Lan.zh) + ex.Message);
                }
            }
             

            JsHelper.AlertAndRedirect(this, CommFunMsg.GetMessage("a0001", Lan.zh), "UserList.aspx");
        }
        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (RenderGridView.GetSelectRows(grid).Count == 0)//如果都不选则导出所有
            {

                gridExport.ExportedRowType = DevExpress.Web.ASPxGridView.Export.GridViewExportedRowType.All;
            }
            grid.Columns["Id"].Visible = false;
            RenderGridView.ExportCvs(gridExport, table);

        }

        protected void btnGroup_Click(object sender, EventArgs e)
        {
            RenderGridView.GridFilter(grid, btnGroup, "显示拖动分组", "取消显示分组", "group");
            LoadUser();
        }
        /// <summary>
        /// 多字段查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grid_ProcessColumnAutoFilter(object sender, ASPxGridViewAutoFilterEventArgs e)
        {
            RenderGridView.ColumnAutoFilter(grid, e);
            LoadUser();
        }

        protected void grid_CustomColumnDisplayText(object sender, ASPxGridViewColumnDisplayTextEventArgs e)
        {
            RenderGridView.ConvertEnum(e);
        }
        protected void pager_PageIndexChanged(object sender, EventArgs e)
        {
            LoadUser();
        }
    }
}